package com.ly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.entity.Activity;
import com.ly.entity.JoinActivity;
import com.ly.mapper.ActivityMapper;
import com.ly.mapper.JoinActivityMapper;
import com.ly.pojo.dto.ActivityDTO;
import com.ly.pojo.vo.ActivityVO;
import com.ly.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : 黑渊白花
 * @ClassName ActivityServiceImpl
 * @date : 2025/2/8 22:32
 * @Description
 */
@Service
@Slf4j
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private JoinActivityMapper joinActivityMapper;


    @Override
    public IPage<ActivityVO> getAllPage(ActivityDTO activityDTO) {
        Integer pageNum = activityDTO.getPageNum();
        Integer pageSize = activityDTO.getPageSize();
        String name = activityDTO.getName();
        String teamName = activityDTO.getTeamName();
        IPage<ActivityVO> page = new Page<>(pageNum,pageSize);
        IPage<ActivityVO> allPage = activityMapper.getAllPage(page,name, teamName);
        return allPage;
    }

    @Override
    public IPage<ActivityVO> getPage(ActivityDTO activityDTO) {
        Integer pageNum = activityDTO.getPageNum();
        Integer pageSize = activityDTO.getPageSize();
        String name = activityDTO.getName();
        Integer status = activityDTO.getStatus();
        Integer teamId = activityDTO.getTeamId();
        IPage<ActivityVO> page = new Page<>(pageNum,pageSize);
        IPage<ActivityVO> stPage = activityMapper.getPage(page,name,status,teamId);
        return stPage;
    }

    @Override
    public IPage<ActivityVO> getAuditPage(ActivityDTO activityDTO) {
        Integer pageNum = activityDTO.getPageNum();
        Integer pageSize = activityDTO.getPageSize();
        String name = activityDTO.getName();
        String teamName = activityDTO.getTeamName();
        IPage<ActivityVO> page = new Page<>(pageNum,pageSize);
        IPage<ActivityVO> auditPage = activityMapper.getAuditPage(page,name, teamName);
        return auditPage;
    }

    @Override
    public List<Activity> getTActivityEnterNum() {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",2).orderByDesc("enter_num").last("limit 5");
        List<Activity> activities = activityMapper.selectList(queryWrapper);
        return activities;
    }

    @Override
    public List<Activity> getEnterNumByTeamId(Integer teamId) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name", "enter_num").eq("status",2);
        queryWrapper.eq("team_id",teamId).orderByDesc("create_time").last("limit 5");
        List<Activity> activities = activityMapper.selectList(queryWrapper);
        return activities;
    }

    @Override
    public List<Activity> getFiveByTeamId(Integer teamId) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",2);
        queryWrapper.eq("team_id",teamId).orderByDesc("create_time").last("limit 5");
        List<Activity> activities = activityMapper.selectList(queryWrapper);
        return activities;
    }

    @Override
    public boolean deleteAll(Integer id) {
        QueryWrapper<JoinActivity> qw = new QueryWrapper<JoinActivity>();
        qw.eq("act_id", id);
        joinActivityMapper.delete(qw);
        activityMapper.deleteById(id);
        return true;
    }

    @Override
    public Long getActNumByTeamId(Integer teamId) {
        QueryWrapper<Activity> qw = new QueryWrapper<Activity>();
        qw.eq("team_id", teamId);
        Long count = activityMapper.selectCount(qw);
        return count;
    }

    @Override
    public ActivityVO getActById(Integer id) {
        ActivityVO actById = activityMapper.getActById(id);
        return actById;
    }

    @Override
    public List<ActivityVO> searchActByType(String typeName) {
        List<ActivityVO> activityVOS = activityMapper.searchActByType(typeName);
        return activityVOS;
    }

    @Override
    public List<ActivityVO> getActsByStuId(Integer stuId) {
        List<ActivityVO> actsByStuId = activityMapper.getActsByStuId(stuId);
        return actsByStuId;
    }
}
